import {
  ADD_COUNTER,
  ADD_TO_CART,
  DELETE_TO_CART
} from './mutation-types'

export default {
  // 唯一的目的就是修改state中状态
  // 每个方法尽可能完成的事件比较单一
  // addToCart(state, payload) {
  [ADD_TO_CART](state, payload) {
    payload.checked = false
    state.cartList.push(payload)
  },
  // AddCounter(state, payload) {
  [ADD_COUNTER](state, payload) {
    // payload新添加的商品
    payload.count++
  },
  // 删除商品
  [DELETE_TO_CART](state, payload) {
    // state.cartList.splice(index, 1)
    for (let i = 0; i < state.cartList.length; i++) {
      if (payload === state.cartList[i]) {
        state.cartList.splice(i, 1)
      }
    }
  }
}